﻿using System;
using System.Net;
using System.IO;
using APIGATEWAY_SDK;

namespace DEMO
{
    class Program
    {
        static void Main(string[] args)
        {
            ServicePointManager.SecurityProtocol =  (SecurityProtocolType)3072;

            demoApp();

            //demoAppLargeFileUpload();
        }

        private static void demoApp()
        {
            Signer signer = new Signer();
            // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
            // 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
            signer.Key = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_AK");
            signer.Secret = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_SK");

            HttpRequest r = new HttpRequest("POST",
                new Uri("https://30030113-3657-4fb6-a7ef-90764239b038.apigw.cn-north-1.huaweicloud.com/app1?query=value"));
            r.headers.Add("x-stage", "RELEASE");
            r.body = "{\"a\":1}";

            HttpWebRequest req = signer.Sign(r);
            try
            {
                var writer = new StreamWriter(req.GetRequestStream());
                writer.Write(r.body);
                writer.Flush();
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                var reader = new StreamReader(resp.GetResponseStream());
                Console.WriteLine(reader.ReadToEnd());
            }
            catch (WebException e)
            {
                HttpWebResponse resp = (HttpWebResponse)e.Response;
                if (resp != null)
                {
                    Console.WriteLine((int)resp.StatusCode + " " + resp.StatusDescription);
                    var reader = new StreamReader(resp.GetResponseStream());
                    Console.WriteLine(reader.ReadToEnd());
                }
                else
                {
                    Console.WriteLine(e.Message);
                }
            }
            Console.WriteLine("----------------");
        }

        private static void demoAppLargeFileUpload()
        {
            String filename = "example.rar";
            if (!File.Exists(filename))
            {
                Console.WriteLine("file not found");
                return;
            }
            Signer signer = new Signer();
            // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
            // 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
            signer.Key = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_AK");
            signer.Secret = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_SK");

            HttpRequest r = new HttpRequest("POST",
                new Uri("https://30030113-3657-4fb6-a7ef-90764239b038.apigw.cn-north-1.huaweicloud.com/app2?query=value"));

            r.headers.Add("x-stage", "RELEASE");
            String hash = Signer.HexEncodeSHA256HashFile(filename);
            r.headers.Add("x-sdk-content-sha256", hash);

            HttpWebRequest req = signer.Sign(r);
            Console.WriteLine(req.Headers.GetValues("x-sdk-date")[0]);
            Console.WriteLine(string.Join(", ", req.Headers.GetValues("authorization")));
            var writer = new BinaryWriter(req.GetRequestStream());
            FileStream fs = new FileStream(filename, FileMode.Open);
            int len = 4096;
            byte[] buffer = new byte[len];
            while (true)
            {
                int readLen = fs.Read(buffer, 0, len);
                if (readLen == 0)
                {
                    break;
                }
                writer.Write(buffer, 0, readLen);
            }
            fs.Dispose();
            writer.Flush();
            try
            {
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                var reader = new StreamReader(resp.GetResponseStream());
                Console.WriteLine(reader.ReadToEnd());
            }
            catch (WebException e)
            {
                HttpWebResponse resp = (HttpWebResponse)e.Response;
                if (resp != null)
                {
                    Console.WriteLine((int)resp.StatusCode + " " + resp.StatusDescription);
                    var reader = new StreamReader(resp.GetResponseStream());
                    Console.WriteLine(reader.ReadToEnd());
                }
                else
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
    }
}
